package com.qingke.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.qingke.dao.ProductManagementDAO;
import com.qingke.pojo.Product;

@WebServlet("/select")
public class SelectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ProductManagementDAO dao=new ProductManagementDAO();
	
    public SelectServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		Integer id=request.getParameter("id")==null||request.getParameter("id").equals("")?null:Integer.valueOf(request.getParameter("id"));
		String name=request.getParameter("name")==null||request.getParameter("name").equals("")?null:request.getParameter("name");
		String description=request.getParameter("description")==null||request.getParameter("description").equals("")?null:request.getParameter("description");
		Double pricelow=request.getParameter("pricelow")==null||request.getParameter("pricelow").equals("")?null:Double.valueOf(request.getParameter("pricelow"));
		Double pricehigh=request.getParameter("pricehigh")==null||request.getParameter("pricehigh").equals("")?null:Double.valueOf(request.getParameter("pricehigh"));
		Integer quantitylow=request.getParameter("quantitylow")==null||request.getParameter("quantitylow").equals("")?null:Integer.valueOf(request.getParameter("quantitylow"));
		Integer quantityhigh=request.getParameter("quantityhigh")==null||request.getParameter("quantityhigh").equals("")?null:Integer.valueOf(request.getParameter("quantityhigh"));
		
		List<Product> list=dao.selectProduct(id, name, description, pricelow, pricehigh, quantitylow, quantityhigh);
		
		Gson gson=new Gson();
		String result=gson.toJson(list);
		
		response.getWriter().write(result);		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
